/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sistemperpustakaan.controller;

import java.awt.image.SampleModel;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import sistemperpustakaan.entity.HariLibur;
import sistemperpustakaan.model.HariLiburModel;
import sistemperpustakaan.tablemodel.HariLiburTableModel;
import sistemperpustakaan.utility.DateUtility;
import sistemperpustakaan.view.harilibur.DialogHariLiburDetail;
import sistemperpustakaan.view.harilibur.FrmHariLiburView;

/**
 *
 * @author EKo
 */
public class HariLiburController {

    FrmHariLiburView hariLiburView;
    DialogHariLiburDetail hariLiburDetail;
    HariLibur hariLibur;
    HariLiburModel model;
    DateUtility du = new DateUtility();

    public HariLiburController() {
    }

    public HariLiburController(FrmHariLiburView hariLiburView, DialogHariLiburDetail hariLiburDetail, HariLibur hariLibur, HariLiburModel model) {
        this.hariLiburView = hariLiburView;
        this.hariLiburDetail = hariLiburDetail;
        this.hariLibur = hariLibur;
        this.model = model;
    }

    public HariLiburController(FrmHariLiburView hariLiburView, HariLiburModel model) {
        this.hariLiburView = hariLiburView;
        this.model = model;
    }

    public HariLibur getHariLibur() {
        return hariLibur;
    }

    public void setHariLibur(HariLibur hariLibur) {
        this.hariLibur = hariLibur;
    }

    public DialogHariLiburDetail getHariLiburDetail() {
        return hariLiburDetail;
    }

    public void setHariLiburDetail(DialogHariLiburDetail hariLiburDetail) {
        this.hariLiburDetail = hariLiburDetail;
    }

    public FrmHariLiburView getHariLiburView() {
        return hariLiburView;
    }

    public void setHariLiburView(FrmHariLiburView hariLiburView) {
        this.hariLiburView = hariLiburView;
    }

    public HariLiburModel getModel() {
        return model;
    }

    public void setModel(HariLiburModel model) {
        this.model = model;
    }

    public void findListHariLibur() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String tanggala = df.format(hariLiburView.getTxtTanggalAwal().getDate());
        String tanggalb = df.format(hariLiburView.getTxtTanggalAkhir().getDate());

        hariLiburView.setTableModel(new HariLiburTableModel());
        hariLiburView.getTableModel().setListHariLibur(model.getListHariLibur(
                tanggala, tanggalb,
                hariLiburView.getTxtKeterangan().getText()));

        hariLiburView.getTableHariLibur().setModel(hariLiburView.getTableModel());
        hariLiburView.setTableView();

        int jumlah = hariLiburView.getTableHariLibur().getRowCount();
        hariLiburView.getLblJumlah().setText(Integer.toString(jumlah));
    }

    public void refreshListHariLibur() {
        hariLiburView.getTxtTanggalAwal().setDate(du.getBeginOfYear());
        hariLiburView.getTxtTanggalAkhir().setDate(du.getEndOfYear());
        hariLiburView.getTxtKeterangan().setText("");
        findListHariLibur();
    }

    public void saveNewHariLibur() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String tanggal = df.format(hariLiburDetail.getTxtTanggal().getDate());
        if (hariLiburDetail.getTxtKeterangan().getText().equals("")) {
            JOptionPane.showMessageDialog(hariLiburDetail, "Field Keterangan masih kosong");
        } else {
            if (model.insertHariLibur(tanggal, hariLiburDetail.getTxtKeterangan().getText())) {
                JOptionPane.showMessageDialog(hariLiburDetail, "Data HariLibur sukses disimpan");
                hariLiburDetail.dispose();
                findListHariLibur();
            } else {
                JOptionPane.showMessageDialog(hariLiburDetail, "Data HariLibur gagal disimpan");
            }
        }

    }

    public void saveUpdateHariLibur() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String tanggal = df.format(hariLiburDetail.getTxtTanggal().getDate());
        String tanggalLama = hariLiburView.getTanggal();
        if (hariLiburDetail.getTxtKeterangan().getText().equals("")) {
            JOptionPane.showMessageDialog(hariLiburDetail, "Field Keterangan masih kosong");
        } else {
            if (model.updateHariLibur(tanggalLama,tanggal, hariLiburDetail.getTxtKeterangan().getText())) {
                JOptionPane.showMessageDialog(hariLiburDetail, "Data HariLibur sukses diupdate");
                hariLiburDetail.dispose();
                findListHariLibur();
            } else {
                JOptionPane.showMessageDialog(hariLiburDetail, "Data HariLibur gagal diupdate");
            }
        }

    }

    public void showDetailHariLibur(String tanggal) {
        try {
            hariLibur = model.getHariLibur(tanggal);
            hariLiburDetail.getTxtTanggal().setDate(hariLibur.getTanggal());
            hariLiburDetail.getTxtKeterangan().setText(hariLibur.getKeterangan());

        } catch (Exception e) {
        }
    }

    public void saveDeleteHariLibur() {
        if (JOptionPane.showConfirmDialog(hariLiburView, "Apakah anda yakin menghapus data ini?", "Hapus data",
                JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION) {
            if (model.deleteHariLibur(hariLiburView.getTanggal())) {
                JOptionPane.showMessageDialog(hariLiburView, "Delete data HariLibur sukses");
                findListHariLibur();
            } else {
                JOptionPane.showMessageDialog(hariLiburView, "Delete data HariLibur gagal");
            }
        }
    }
}
